import {message} from "antd";



const addMarkers = (markerData, myMap) => {
  try {
    let layers = [];
    let myIcon;
    for (let i = 0; i < markerData.length; i++) {
      let {lat, lon} = markerData[i];
      myIcon = L.icon({
        iconUrl: defaulImage,  // jpg png gif都可以
        iconSize: [28, 28]
      });
      let marker = L.marker([lat, lon], {icon: myIcon});
      marker.on('mouseover', rainSituationTip(marker, {
        name: markerData[i]['name'],
      }));
      layers.push(marker);
      marker.on('click', function (e) {
        console.log(e.target);
        //可以传任意对象
        // broadcast(EVENT_mapMarker_click, {e: e, lon: lon, lat: lat, name: name, type: type, stnm: stnm, id: stcd});
      });
      marker.on('mouseout', function (e) {
        marker.closePopup();
      });
    }
    return addGroupLayer(layers, myMap);
  } catch (e) {
  }
};


/**
 * 地图上添加图层
 * @param layers 图层数组
 */
export const addGroupLayer = (layers,map) => {
  try {
    let myGroup = L.layerGroup(layers);
    map.addLayer(myGroup);
    return myGroup;
  }catch (e) {
    message.info('渲染图层数组失败')
  }
};

/**
 * 地图上移除图层
 * @param layers 图层数组
 */
export const removeGroupLayer = (layerGroup,map) => {
  try {
    if (layerGroup) {
      map.removeLayer(layerGroup);
    }
  }catch (e) {
    message.info('移除图层失败')
  }
};

/**
 * 定位
 * @param bounds
 */
export  const locateBounds = (obj,map)=>{
  try {
    map.fitBounds(obj.getBounds());
  }catch (e) {
    message.info('定位失败')
  }
}
